package com.vcms.controller.machine;

import java.awt.Frame;

import com.vcms.controller.system.MainController;
import com.vcms.entity.door.Door;
import com.vcms.entity.store.itemtype.Coin;
import com.vcms.exception.VMCSException;
import com.vcms.patterns.observer.Observable;
import com.vcms.patterns.observer.Observer;
import com.vcms.view.simulation.machine.MachinerySimulatorPanel;
import com.vcms.view.simulation.system.SimulatorControlPanel;
import com.vcms.view.simulation.uicomponent.dialog.MessageDialog;

public class MachinaryController implements Observer {

	private MainController mainController;
	private Door door;
	private MachinerySimulatorPanel machineSimulatorPanel;
	
	public MachinaryController(MainController mctrl) {
		this.mainController = mctrl;
	}
	
	public void initialize() throws VMCSException {
		door = new Door();
	}
	
	public void setDoorState(boolean state) {
		door.setState(state);
		displayDoorState();
	}

	public void displayDoorState() {
		if (machineSimulatorPanel == null)
			return;
			machineSimulatorPanel.setDoorState(door.isDoorClosed());
	}
	
	
	public MachinerySimulatorPanel getMachinerySimulatorPanel() {
		return machineSimulatorPanel;
	}

	public void displayMachineryPanel() {
		SimulatorControlPanel scp = mainController.getSimulatorControlPanel();
		if (machineSimulatorPanel == null)
			machineSimulatorPanel = new MachinerySimulatorPanel((Frame) scp, this);

		machineSimulatorPanel.display();
		scp.setActive(SimulatorControlPanel.ACT_MACHINERY, false);
	}
	
	public void closeMachineryPanel() {
		if (machineSimulatorPanel == null)
			return;

		if (!isDoorClosed()) {
			@SuppressWarnings("unused")
			MessageDialog msg = new MessageDialog(machineSimulatorPanel, "Please Lock the Door before You Leave");
			return;
		}
		machineSimulatorPanel.dispose();
		SimulatorControlPanel scp = mainController.getSimulatorControlPanel();
		scp.setActive(SimulatorControlPanel.ACT_MACHINERY, true);
		return;
	}
	
	public boolean isDoorClosed() {
		return door.isDoorClosed();
	}
	
	public void displayDrinkStock() throws VMCSException {
		if (machineSimulatorPanel == null)
			return;

		machineSimulatorPanel.getDrinksStoreDisplay().update();
	}
	
	public void displayCoinStock() throws VMCSException {
		if (machineSimulatorPanel == null)
			return;

		machineSimulatorPanel.getCashStoreDisplay().update();
	}
	
	public void closeDown(){
		if (machineSimulatorPanel != null)
			machineSimulatorPanel.dispose();
	}
	
	public void dispenseDrink(int idx) throws VMCSException {
		this.mainController.getStoreController().dispenseDrink(idx);
	}
	
	public boolean giveChange(int coinIndex, int numberOfCoin){
		try{
			this.getMainController().getStoreController().giveChange(coinIndex, numberOfCoin);
			if (machineSimulatorPanel != null)
				machineSimulatorPanel.getCashStoreDisplay().update();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public void storeCoin(Coin coin) throws VMCSException {
		this.getMainController().getStoreController().storeCoin(coin);
		
		if (machineSimulatorPanel != null)
			machineSimulatorPanel.getCashStoreDisplay().update();
	}
	
	

	public MainController getMainController() {return mainController;}

	@Override
	public void update(Observable observable, Object object, int selection) throws VMCSException {
		if (machineSimulatorPanel != null)
			machineSimulatorPanel.getDrinksStoreDisplay().update();
		
		System.out.println("MachinaryController - update");
	}

}
